#include "display_module.h"
#include "stdio.h"
#include "stdbool.h"

// 数据缓存
#define MAX_POINTS 85
static float voltage_data[MAX_POINTS];   // 电压数据缓存
static float current_data[MAX_POINTS];   // 电流数据缓存
static uint8_t data_index = 0;           // 数据索引
static bool full_buffer = false;

// 显示区域定义
#define Start_x 0
#define Start_y 0
#define End_x 159
#define End_y 79

// 坐标映射函数
#define VOLTAGE_TO_Y(v) (75 - ((v)/35.0)*57)  // 0-35V映射到Y轴
#define CURRENT_TO_Y(i) (75 - ((i)/10.0)*57)  // 0-10A映射到Y轴


/******************************************************************************
      函数说明：绘制静态菜单界面元素
      入口数据：无
      返回值：  无
******************************************************************************/
static void draw_menu_interface(void) {
    LCD_Fill(Start_x, Start_y, End_x, Start_y + 18, LIGHTBLUE);
    LCD_ShowString(Start_x + 30, Start_y, (u8 *)"Power Monitor", WHITE, LIGHTBLUE, 16, 0);

    LCD_Fill(Start_x, Start_y + 19, 74, Start_y + 37, BROWN);
    LCD_ShowString(Start_x + 1, Start_y + 20, (u8 *)"U/V:", YELLOW, BROWN, 16, 0);

    LCD_Fill(Start_x, Start_y + 38, 74, Start_y + 56, GREEN);
    LCD_ShowString(Start_x + 1, Start_y + 39, (u8 *)"I/A:", BLUE, GREEN, 16, 0);

    LCD_Fill(Start_x, Start_y + 57, 74, Start_y + 75, BLUE);
    LCD_ShowString(Start_x + 1, Start_y + 58, (u8 *)"P/W:", WHITE, BLUE, 16, 0);

    LCD_DrawRectangle(75, 18, 159, 75, WHITE);
    LCD_DrawLine(75, 75, 159, 75, WHITE);
    LCD_DrawLine(75, 18, 75, 75, WHITE);

    for(uint8_t i = 0; i <= 8; i++) {
        uint16_t x = 75 + i*10;
        LCD_DrawLine(x, 74, x, 72, WHITE);
    }

    for(uint8_t i = 0; i <= 11; i++) {
        uint16_t y = 75 - i*5;
        LCD_DrawLine(75, y, 77, y, WHITE);
    }
}

/******************************************************************************
      函数说明：内部实现刷新数据显示功能
      入口数据：voltage 待显示电压值（0-35V）
                current 待显示电流值（0-10A）
                power 待显示功率值（0-100W）
      返回值：  无
******************************************************************************/
static void refresh_data_display(float voltage, float current, float power) {
    LCD_ShowFloatNum1(33, 20, voltage, 4, YELLOW, BROWN, 16);
    LCD_ShowFloatNum1(33, 39, current, 4, BLUE, GREEN, 16);
    LCD_ShowFloatNum1(33, 58, power, 4, WHITE, BLUE, 16);

    voltage_data[data_index] = voltage;
    current_data[data_index] = current;

    LCD_Fill(77, 19, 159, 74, BLACK);

    for(uint8_t i = 0; i < (full_buffer ? MAX_POINTS : data_index + 1); i++) {
        uint8_t idx = (data_index + MAX_POINTS - i) % MAX_POINTS;
        int x = 158 - i;
        int y_v = VOLTAGE_TO_Y(voltage_data[idx]);
        int y_i = CURRENT_TO_Y(current_data[idx]);

        if(x >= 77 && y_v >= 18 && y_v <= 72) {
            LCD_DrawPoint(x, y_v, RED);
        }
        if(x >= 77 && y_i >= 18 && y_i <= 72) {
            LCD_DrawPoint(x, y_i, CYAN);
        }
    }

    if(full_buffer) {
        data_index = (data_index + 1) % MAX_POINTS;
    } else {
        if(data_index < MAX_POINTS - 1) {
            data_index++;
        } else {
            full_buffer = true;
            data_index = 0;
        }
    }
}

/******************************************************************************
      函数说明：初始化显示模块，完成LCD初始化及界面绘制
      入口数据：无
      返回值：  无
******************************************************************************/
void display_module_init(void) {
    LCD_Init();
    LCD_Fill(0, 0, LCD_W, LCD_H, BLACK);
    draw_menu_interface();
}


/******************************************************************************
      函数说明：刷新主界面显示（公共接口）
      入口数据：voltage 待显示电压值（0-35V）
                current 待显示电流值（0-10A）
                power 待显示功率值（0-100W）
      返回值：  无
******************************************************************************/
void display_module_refresh(float voltage, float current, float power) {
    refresh_data_display(voltage, current, power);
}
